package com.hongxun.budget.ui.activity.ys;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.utils.DateUtils;
import com.hongxun.budget.utils.T;
import com.hongxun.budget.utils.TestingUtils;
import com.hongxun.greenentity.ys.EProblemexistEntity;

import butterknife.BindView;
import butterknife.OnClick;

public class EditeYsActivity extends BaseActivity {


    @BindView(R.id.right_save)
    Button rightSave;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.ys)
    TextView ys;
    @BindView(R.id.wh)
    TextView wh;
    @BindView(R.id.finder)
    EditText finder;
    @BindView(R.id.find_time)
    TextView findTime;
    @BindView(R.id.jy)
    EditText jy;
    @BindView(R.id.result)
    EditText result;
    @BindView(R.id.process_man)
    EditText processMan;
    @BindView(R.id.process_time)
    TextView processTime;
    EProblemexistEntity e;
    int index = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_edite_ys;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        rightSave.setVisibility(View.VISIBLE);
        Intent intent = getIntent();
        index = intent.getIntExtra("index", 0);
        e = (EProblemexistEntity) intent.getSerializableExtra("problem");
        name.setText(e.getCommon());
        ys.setText(TextUtils.isEmpty(e.getAccepter()) ? "未验收" : "已验收");
        wh.setText(e.getAnalysis());
        finder.setText(e.getFinder());
        findTime.setText(DateUtils.getDateToStringNoHH(e.getFindtime()));
        jy.setText(e.getNote());
        result.setText(e.getResults() == null ? "" : e.getResults());
        processMan.setText(e.getHandler() == null ? "" : e.getHandler());
        processTime.setText(e.getHandletime() == null ? "" : DateUtils.getDateToStringNoHH(e.getHandletime()));
    }


    @OnClick({R.id.right_save, R.id.find_time, R.id.process_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.right_save:
                save();
                break;
            case R.id.find_time:
                DateUtils.showDatePickerDialog(this, R.style.MyDatePickerDialogTheme, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
                        findTime.setText(year + "-" + (month + 1) + "-" + dayOfMonth);
                    }
                });
                break;
            case R.id.process_time:
                DateUtils.showDatePickerDialog(this, R.style.MyDatePickerDialogTheme, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
                        processTime.setText(year + "-" + (month + 1) + "-" + dayOfMonth);
                    }
                });
                break;
        }
    }

    void save() {
        String f_er = finder.getText().toString();
        if (!TestingUtils.isEmpty(context, f_er, "发现人")) return;
        e.setFinder(f_er);
        String f_time = findTime.getText().toString();
        e.setFindtime(DateUtils.getStringToDateNoHH(f_time));
        e.setNote(getText(jy));
        e.setResults(getText(result));
        e.setHandler(getText(processMan));
        String p_time = getText(processTime);
        if (p_time != null) e.setHandletime(DateUtils.getStringToDateNoHH(p_time));
        String n = getText(name);
        if (!TestingUtils.isEmpty(context, f_er, "问题描述")) return;
        e.setCommon(n);
        Intent intent = new Intent();
        intent.putExtra("problem", e);
        intent.putExtra("index", index);
        setResult(Activity.RESULT_OK, intent);
        finish();
        T.showLong(context, "保存成功");
    }
}
